package com.ruoyi.system.interfacechannel.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.interfacechannel.domain.InterfaceChannel;
import com.ruoyi.system.interfacechannel.service.IInterfaceChannelService;
import com.ruoyi.system.model.domain.SelectPush;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 接口渠道表(interface_channel)表控制层
 *
 * @author zyy
 * @date 2024-01-10 10:18:26
 */
@RestController
@RequestMapping("/interfaceChannel")
public class InterfaceChannelController extends BaseController {
    /**
     * 服务对象
     */
    @Autowired
    private IInterfaceChannelService interfaceChannelService;

    /**
     * 分页查询所有数据
     *
     * @param interfaceChannel 查询实体
     * @return 所有数据
     */
    @GetMapping("/list")
    public TableDataInfo list(InterfaceChannel interfaceChannel) {
        LambdaQueryWrapper<InterfaceChannel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(interfaceChannel.getName()), InterfaceChannel::getName,
                interfaceChannel.getName());
        queryWrapper.eq(StringUtils.isNotNull(interfaceChannel.getIsShow()), InterfaceChannel::getIsShow,
                interfaceChannel.getIsShow());
        queryWrapper.eq(StringUtils.isNotNull(interfaceChannel.getChannelType()), InterfaceChannel::getChannelType,
                interfaceChannel.getChannelType());
        List<Long> gameIdList = interfaceChannel.getGameIdList();
        if (CollectionUtils.isNotEmpty(gameIdList)) {
            for (Long gameId : gameIdList) {
                queryWrapper.apply("FIND_IN_SET({0}, gameIds)", gameId);
            }
        }
        queryWrapper.orderByAsc(InterfaceChannel::getSort);
        if (!Objects.equals(interfaceChannel.getIsPage(), 2)) {
            startPage();
        }
        List<InterfaceChannel> list = interfaceChannelService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(interfaceChannelService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param interfaceChannel 实体对象
     * @return 新增结果
     */
    @Log(title = "InterfaceChannel", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult insert(@RequestBody InterfaceChannel interfaceChannel) {
        LambdaQueryWrapper<InterfaceChannel> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.select(InterfaceChannel::getName);
        queryWrapper.eq(InterfaceChannel::getCode, interfaceChannel.getCode());
        List<InterfaceChannel> list = interfaceChannelService.list(new LambdaQueryWrapper<InterfaceChannel>()
                .select(InterfaceChannel::getId)
                .eq(InterfaceChannel::getCode, interfaceChannel.getCode()));
        if (CollectionUtils.isNotEmpty(list)) {
            throw new GlobalException("此code已在" + list.get(0).getName() + "存在");
        }
        return toAjax(interfaceChannelService.save(interfaceChannel));
    }

    /**
     * 修改数据
     *
     * @param interfaceChannel 实体对象
     * @return 修改结果
     */
    @Log(title = "InterfaceChannel", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult update(@RequestBody InterfaceChannel interfaceChannel) {
        LambdaQueryWrapper<InterfaceChannel> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.select(InterfaceChannel::getId, InterfaceChannel::getName);
        queryWrapper.eq(InterfaceChannel::getCode, interfaceChannel.getCode());
        List<InterfaceChannel> list = interfaceChannelService.list(new LambdaQueryWrapper<InterfaceChannel>()
                .select(InterfaceChannel::getId)
                .eq(InterfaceChannel::getCode, interfaceChannel.getCode()));
        if (CollectionUtils.isNotEmpty(list)) {
            InterfaceChannel interfaceChannel1 = list.get(0);
            if (list.size() > 1) {
                throw new GlobalException("此code已在" + interfaceChannel1.getName() + "存在");
            } else {
                if (!Objects.equals(interfaceChannel1.getId(), interfaceChannel.getId())) {
                    throw new GlobalException("此code已在" + interfaceChannel1.getName() + "存在");
                }
            }
        }
        return toAjax(interfaceChannelService.updateById(interfaceChannel));
    }

    /**
     * 删除数据
     *
     * @param ids 主键结合
     * @return 删除结果
     */
    @Log(title = "InterfaceChannel", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(interfaceChannelService.removeByIds(Arrays.asList(ids)));
    }

    /**
     * 新增【请填写功能名称】
     */
    @GetMapping(value = "/getBySalePartnerId")
    public AjaxResult getBySalePartnerId(SelectPush selectPush) {
        return AjaxResult.success(interfaceChannelService.getBySalePartnerId(selectPush));
    }
}

